package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Question;
import cn.wolfcode.mapper.ExamMapper;
import cn.wolfcode.mapper.QuestionMapper;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;

    @Autowired
    private IQuestionService questionService;

    @RequirePermission(name = "试卷页面",expression = "exam:list")
    @RequestMapping("/list")
    public String list(Model model){
        Exam exam = examService.selectById(4L);

        //题目
        List<Question> oneList = new ArrayList<>();
        List<Question> manyList = new ArrayList<>();
        List<Question> judgeList = new ArrayList<>();
        List<Long> longs = examService.selectQueIdByExamId(exam.getId());

        //分数
        int oneCount = 0;
        int manyCount = 0;
        int judgeCount = 0;

        for (Long aLong : longs) {
            Question question = questionService.selectByQuestionId(aLong,exam.getId());
            if (question.getType() == 1) {
                oneList.add(question);
                oneCount += question.getScore();
                exam.setOneScore(oneCount);
            } else if (question.getType() == 2) {
                manyList.add(question);
                manyCount += question.getScore();
                exam.setManyScore(manyCount);
            } else if (question.getType() == 3) {
                judgeList.add(question);
                judgeCount += question.getScore();
                exam.setJudgeScore(judgeCount);
            }
        }

        model.addAttribute("oneList", oneList);
        model.addAttribute("manyList", manyList);
        model.addAttribute("judgeList", judgeList);
        model.addAttribute("exam", exam);
        return "exam/list";
    }

    @RequirePermission(name = "试卷新增",expression = "exam:input")
    @RequestMapping("/input")
    public String input(Model model,Exam exam){
        return "exam/input";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "试卷删除",expression = "exam:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            examService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "试卷新增/编辑",expression = "exam:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Exam exam){
        if (exam.getId() != null) {
            examService.update(exam);
        }else {
            examService.save(exam);
        }
        return new JsonResult();
    }
}
